﻿#ifndef LISTUTIL_H
#define LISTUTIL_H
#include <QList>



class ListUtil {
public:
    ListUtil();
    template<typename T>
    static void add(QList<T> &list, QList<T> listAdd) {
        for (int i = 0; i < listAdd.size(); i++) {
            list.append(listAdd[i]);
        }
    }

    template<typename T1, typename T2>
    static QList<T2> cast(QList<T1> list) {
        QList<T2> res;
        for (int i = 0; i < list.size(); i++) {
            res.append((T2)list[i]);
        }
        return res;
    }

    template<typename T>
    static int count(QList<T> list, std::function<bool()> func) {
        QList<T> filterRes = filter(list, func);
        int res = filterRes.size();
        delete filterRes;
        return res;
    }

    template<typename T>
    static QList<T> filter(QList<T> list, std::function<bool(T)> func) {
        QList<T> res;
        for(int i=0;i<list.size();i++){
            T item = list[i];
            if (func(item) == true) {
                res.append(item);
            }
        }
        return res;
    }

    template<typename T1, typename T2>
    static QList<T2> map(QList<T1> list, std::function<T2(T1)> func) {
        QList<T2> res;
        for(int i=0;i<list.size();i++){
            T1 item = list[i];
            res.append(func(item));
        }
        return res;
    }
};

#endif // LISTUTIL_H
